﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Snake.Database;
using Snake.Mapping;

namespace Snake
{
    public abstract class SessionFactory : ISessionFactory
    {
        private readonly MappingBuilder _builder = new MappingBuilder();
        private IEnumerable<EntityMap> _entities;
        private readonly string _connectionString;

        protected SessionFactory(string connectionString)
        {
            _connectionString = connectionString;
        }

        protected abstract void OnMapping(IMappingBuilder builder);

        public ISession Create()
        {
            DbConnection connection = new SqlConnection(_connectionString);
            DbCommand command = new SqlCommand();//现在是执行SQL command
            command.Connection = connection;
            if (_entities == null)
            {
                OnMapping(_builder);
                _entities = _builder.Build();
            }
            return new Session(_entities, connection);
        }
    }
}
